<?php
/**
 * 处理万年历请求的类
 */
require_once ('handle.class.php');

class wnl extends handle {
	
	private $_year;
	private $_month;
	private $_day;
	private $_date;
	private $_jie;
	private $_type;
	
	private $_response_day;
	private $_response_month;

	function preHandle() {
		if(!empty($this->_request_data)) {
			if(preg_match("/^([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})$/u", $this->_request_data, $date)) {
				if(count($date)==4 && !empty($date[1]) && !empty($date[2]) && !empty($date[3])) {
					$this->_year=intval($date[1]);
					$this->_month=intval($date[2]);
					$this->_day=intval($date[3]);
					if(!checkdate($this->_month, $this->_day, $this->_year)) {
						//日期不合法
						$this->_error_code = 102;
						return false;
					} elseif($this->_year<1600 || $this->_year>2099) {
						//年份暂未提供服务
						$this->_error_code = 103;
						return false;
					}
					$this->_type = 1;
					return true;
				} else {
					//日期格式错误
					$this->_error_code = 101;
					return false;
				}
			} elseif(preg_match("/^([0-9]{4})\/(.+?)$/u", $this->_request_data, $date)) {
				if(count($date)==3 && !empty($date[1]) && !empty($date[2])) {
					$this->_year=intval($date[1]);
					$this->_jie=trim($date[2]);
					if($this->_year<1600 || $this->_year>2099) {
						//年份暂未提供服务
						$this->_error_code = 103;
						return false;
					}
					$this->_type = 2;
					return true;
				} else {
					//节日|节气格式错误
					$this->_error_code = 104;
					return false;
				}
			} else {
				//数据格式错误
				$this->_error_code = 105;
				return false;
			}
		} else {
			$date = date("Y-n-j",time());
			list($this->_year,$this->_month,$this->_day) = explode('-',$date);
			$this->_type = 1;
			return true;
		}
		
		return true;
	}
	
	
	
	/* (non-PHPdoc)
	 * @see handle::handleMain()
	 */
	function handleMain() {
		// TODO Auto-generated method stub
		switch ($this->_type) {
			case 1:
				$this->_table = "century" . intval($this->_year/100);
				$this->_date = $this->_year.'-'.$this->_month.'-'.$this->_day;
				$this->_response_day = $this->_db->fetchrow("SELECT * FROM {$this->_table} WHERE cdate='{$this->_date}'");
				$monthstr = $this->_year."-".$this->_month;
				$this->_response_month = $this->_db->fetchrow("SELECT * FROM month WHERE mdate='{$monthstr}'");
				if(empty($this->_response_day) || empty($this->_response_month)) {
					//未定义错误
					$this->_error_code = 999;
					return false;
				}
				return true;
				break;
			
			case 2:
				$this->_table = "century" . intval($this->_year/100);
				//echo "SELECT * FROM {$this->_table} WHERE cdate LIKE '{$this->_year}%' AND (jieri LIKE '%{$this->_jie}%' OR jieqi LIKE '%{$this->_jie}%')<br/>\n";
				$this->_response_day = $this->_db->fetchrow("SELECT * FROM {$this->_table} WHERE cdate LIKE '{$this->_year}%' AND (jieri LIKE '%{$this->_jie}%' OR nongli LIKE '%{$this->_jie}%' OR jieqi LIKE '%{$this->_jie}%')");
				//print_r($this->_response_day);
				if(empty($this->_response_day)) {
					//未定义错误
					$this->_error_code = 999;
					return false;
				}
				return true;
				break;
			
			default:
				$this->_error_code = 999;
				return false;
			break;
		}
		
	}

	function run() {
		//预处理数据
		if(!($this->preHandle() && $this->handleMain())) return false;
		return true;
	}

	function get_Response_data() {
		switch ($this->_type) {
			case 1:
				$this->_response_data = "【".$this->_year."年".$this->_month."月".$this->_day."日】 ".$this->_response_day['xingqi']." 农历".$this->_response_day['nongli']." [节日]：".$this->_response_day['jieri']." [节气]：".$this->_response_day['jieqi']." [星座]：".$this->_response_day['xingzuo']." [干支]：".$this->_response_day['ganzhi']." [二十八宿]：".$this->_response_day['ershibasu']." [甲子纳音]：".$this->_response_day['jiazinayin'];
			break;
			
			case 2:
				$this->_response_data = "【".$this->_response_day['cdate']."】 ".$this->_response_day['xingqi']." 农历".$this->_response_day['nongli']." [节日]：".$this->_response_day['jieri']." [节气]：".$this->_response_day['jieqi']." [星座]：".$this->_response_day['xingzuo']." [干支]：".$this->_response_day['ganzhi']." [二十八宿]：".$this->_response_day['ershibasu']." [甲子纳音]：".$this->_response_day['jiazinayin'];
			break;
			
			default:
				$this->_response_data = "未知错误";
			break;
		}
		return $this->_response_data;
	}
	
	function get_Response_day() {
		$this->_response_day['year'] = $this->_year;
		$this->_response_day['month'] = $this->_month;
		$this->_response_day['day'] = $this->_day;
		return $this->_response_day;
	}
	
	function get_Response_month() {
		return $this->_response_month;
	}
	
	function handleQueue() {
		// TODO Auto-generated method stub
		
	}
}

?>